<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="68px"
    >
      <el-form-item label="分类名" prop="category">
        <el-select
            v-model="queryParams.category"
            placeholder="请选择分类名"
            clearable
            class="!w-240px"
            @change="categoryChanged"
        >
          <el-option
              v-for="dict in categoryOptions"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="品牌" prop="brand">
        <el-select
            v-model="queryParams.brand"
            placeholder="请选择品牌"
            clearable
            class="!w-240px"
            @change="brandChanged"
        >
          <el-option
              v-for="dict in brandOptions"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="型号" prop="model">
        <el-select
            v-model="queryParams.model"
            placeholder="请选择型号"
            clearable
            class="!w-240px"
        >
          <el-option
              v-for="dict in modelOptions"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <!--      <el-table-column label="id" align="center" prop="id" />-->
      <el-table-column label="分类名" align="center" prop="category">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.ECG_DEV_CATEGORY" :value="scope.row.category" />
        </template>
      </el-table-column>
<!--
      <el-table-column label="品牌" align="center" prop="brand" width="100px">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.ECG_DEV_BRAND" :value="scope.row.brand" />
        </template>
      </el-table-column>
      <el-table-column label="型号" align="center" prop="model" />
-->
      <el-table-column label="使用状态" align="center" prop="lost" >
        <template #default="scope">
          <span>{{tranlateDevState(scope.row.state)}}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="采购日期" align="center" prop="purchaseDate" :formatter="dateFormatter2" width="120px"/>-->
      <el-table-column label="数量" align="center" prop="devCount" />
    </el-table>
  </ContentWrap>
</template>

<script setup lang="ts">
import {getStrDictOptions, DICT_TYPE, DictDataType} from '@/utils/dict'
import {dateFormatter, dateFormatter2} from '@/utils/formatTime'
import download from '@/utils/download'
import {DeviceApi, DeviceStatisticVO, DeviceVO, DevModelApi, OptionsVO} from '@/api/ecg/devmanage'
import DeviceForm from './DeviceForm.vue'

/** 设备 列表 */
defineOptions({ name: 'DeviceStatistic' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<DeviceStatisticVO[]>([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  purchaseDate: [],
  createTime: [],
  devId: undefined,
  category: undefined,
  brand: undefined,
  model: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

const categoryOptions = ref<DictDataType[]>([])
const brandOptions = ref<OptionsVO[]>([])
const modelOptions = ref<OptionsVO[]>([])

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await DeviceApi.deviceStatistic(queryParams)
    console.info( data )
    list.value = data
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await DeviceApi.deleteDevice(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await DeviceApi.exportDevice(queryParams)
    download.excel(data, '设备.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

const tranlateDevState = (state) => {
  if (state === 0) return "空闲";
  else if (state=== 5) return "已领用";
  else if (state=== 10) return "已装机";
  else if (state=== 20) return "已遗失";
  else if (state=== 30) return "维修中";
  else if (state=== 40) return "已报废";
}

const categoryChanged = async () => {
  const data = await DevModelApi.getBrandOption(queryParams.category!)
  brandOptions.value = data

  queryParams.brand = ''
  queryParams.model = ''

  //queryParams.brand = brandOptions.value.length === 0 ? "" : brandOptions.value[0].value
  //brandChanged()
}

const brandChanged = async () => {
  const data = await DevModelApi.getModelOption(queryParams.category!, queryParams.brand!)
  modelOptions.value = data

  queryParams.model = ''

  //queryParams.model = modelOptions.value.length === 0 ? "" : modelOptions.value[0].value
}

/** 初始化 **/
onMounted( async () => {
  const data = await getStrDictOptions(DICT_TYPE.ECG_DEV_CATEGORY)
  categoryOptions.value = data

  getList()
})
</script>